#!/bin/bash

function print_usage()
{
   echo "Usage: $0 <center> <dbname> <dblogin> <dbpassword> <oplpath>"
   echo "Insert OPL Data into the database"
}

function get_path()
{
    case "$1" in
	0)
	    echo "../"
	    ;;
	
	8)
	    echo "skipped"
	    ;;
	1,2,3,12)
	    echo ""
	    ;;

	*)
	    echo "error"
	    ;;
    esac
}

file_type=$1
center=$2
dbname=$3
dblogin=$4
dbpassword=$5
oplpath=$6
#oplpath="/data/samba_share/global/opl"

if [ "x$oplpath" == "x" ]; then
   echo "`date +'%Y/%m/%d %H:%M:%S'` [ERROR] All arguments not available."
   print_usage
   exit 1
fi

lockfile="/tmp/import_opl.lock" 
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Checking lock file $lockfile..."
if [ -f $lockfile ]; then 
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Lock file exits! Aborting..." 
    exit
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Creating lock file..."
`touch /tmp/import_opl.lock` 

let "total_processed_file_counter=0"
let "skipped_processed_file_counter=0"
let "error_processed_file_counter=0"
let "new_processed_file_counter=0"

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Processing... "

tmpfile1=/tmp/importopl.tmp.1
tmpfile2=/tmp/importopl.tmp.2

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Listing files from $oplpath/imported... "

#for f in `ls /data/samba_share/archives/huizhou/client_file/20070520/*.txt`
find >$tmpfile1 $oplpath/imported/ -maxdepth 1 -name "*.000"

C=`cat $tmpfile1 | wc -l`
if [ $C -eq 0 ]; then
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: No file found."
else

    cat $tmpfile1 | sed >$tmpfile2 s/\ [\ ]*/\+/g
    
    for f in `cat $tmpfile2`
      do
      
      f=`echo $f | sed s/\+/\ /g`
      
      #/data/s/data/scripts/import_opl.pl --log /data/home/simgen/import_opl.log "$f" $1 $2 $3
      let "total_processed_file_counter=$total_processed_file_counter+1"
      
      /data/scripts/import_opl.pl --log /data/home/simgen/import_opl.sh.log $file_type $center "$f" $dbname $dblogin $dbpassword
      
      res=$?
      filepath=""

      # res=0  -> process ok
      # res=1  -> function intro failed
      # res=2  -> function parse args failed
      # res=3  -> db connect failed
      # res=7  -> wrong file 
      # res=8  -> file already processed
      # res=9  -> data insert failed
      # res=10 -> opl_id retrieve failed
      # res=11 -> imei insert failed
      # res=12 -> function main failed
      # res=13 -> com_ref_id retrieve failed
      # res=14 -> client_id insert failed
      # res=15 -> event add failed
      # res=16 -> event_id retrieve failed
      # res=17 -> imei_additional_info_id insert failed
      # res=18 -> OPL contains duplicated imei

      basename=`basename $f`

      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ERROR CODE=$res"

      if [ $res -eq 3 -o $res -eq 1 -o $res -eq 2 -o $res -eq 12 ]; then
	  let "error_processed_file_counter=$error_processed_file_counter+1"

      elif [ $res -eq 8 ]; then
	  let "skipped_processed_file_counter=$skipped_processed_file_counter+1"
	  filepath="skipped"
	  #echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be moved to $oplpath."
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be removed."
	  `mv "$f" $oplpath/$filepath`
	  #`rm -f "$f"`
  
      elif [ $res -eq 9 -o $res -eq 10 -o $res -eq 11 -o $res -eq 7 -o $res -eq 13 -o $res -eq 14 -o $res -eq 15 -o $res -eq 16 -o $res -eq 17 -o $res -eq 18 ]; then
	  let "error_processed_file_counter=$error_processed_file_counter+1"
	  filepath="error"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be moved to $oplpath/$filepath."
	  `mv "$f" $oplpath/$filepath`

      elif [ $res -eq 0 ]; then
	  let "new_processed_file_counter=$new_processed_file_counter+1"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Done."
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be moved to $oplpath."
	  #echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be removed."
	  `mv /tmp/$basename.tmp $oplpath/$basename`
	  `rm -f "$f"`
	  #rm -f "$f"`

      else 
	  let "error_processed_file_counter=$error_processed_file_counter+1"
	  filepath="error"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File $basename will be moved to $oplpath/$filepath..."
	  `mv "$f" $oplpath/$filepath`

      fi
      
    done

fi
    
`rm -f $tmpfile1 $tmpfile2`
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: End of Process."
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ### Summary: #################################################"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Total OPL file Processed: $total_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:         Included skipped: $skipped_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:           Included error: $error_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:             Included new: $new_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ##################################"

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Deleting lock file..."
`rm /tmp/import_opl.lock` 

error=`grep "\[ERROR\]" /tmp/import_opl.sh.log`
res=$?
if [ $res -eq 0 ]; then
    /data/scripts/sendmail.pl -f multisimgen.support@tcl.com -t raffi.semerciyan@jrdcom.com -s "Multisimgen >import_opl.sh< notification" /tmp/import_opl.sh.log
fi
